-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable service worker by default #3817
Conversation
@@ -2,7 +2,16 @@ import React from 'react'; | |||
import ReactDOM from 'react-dom'; | |||
import './index.css'; | |||
import App from './App'; | |||
import registerServiceWorker from './registerServiceWorker'; | |||
import registerServiceWorker, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importing both of these results in a lint error because only one of them is ever used. We could override eslint for this line, we could only import one and update the comment below (ugh) or we could do something else entirely like only using registerServiceWorker
and passing it a boolean to enable/disable.
How about using import * as serviceWorker from './serviceWorker';
// ...
serviceWorker.unregister(); |
Good suggestion Dan. I'll make that change. |
If I use the Is it okay to change |
I'd say registerServiceWorker could use some edits, at least in the comments to tell the dev it's opt-in and how that opt-in works. What about adding a new function in registerServiceWorker.js to help devs destroy a service worker if their users are stuck with it by mistake? AFAIK .unregister() doesn't execute unless the rendered service-worker.js has changed. Was looking at adding something like this to my own rSW.js |
@iansu I'm proposing to make both functions named exports. |
Thanks for taking this on, @iansu! As it's currently implemented, export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister();
});
}
}
I touched some of the code in |
@@ -18,7 +18,7 @@ const isLocalhost = Boolean( | |||
) | |||
); | |||
|
|||
export default function register() { | |||
export function register() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default export still needs to be registerServiceWorker too though (even if both are named exports too) for it to be backwards compatible (even if this lands to a major version update 2.0.0, it would be unneccessary to break everyones build for such a tiny change).
Still partial to passing the boolean in myself: #2554 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't break anyone's build because this part is copied into every project. Even if we changed completely the API between them, we could've shipped that in any patch release IMO.
Thanks for the feedback everyone. I think I've addressed it all. |
|
||
ReactDOM.render(<App />, document.getElementById('root')); | ||
registerServiceWorker(); | ||
|
||
// By default we make sure that no service worker is registered. If you would |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People reading this probably don't know what service worker is.
Can you rewrite this so it's a bit less wordy, and also understandable to most folks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that's a challenge! 😀
I guess we can remove the first sentence and maybe link to that Google page from the docs about what a service worker is. Alternatively we could just link to the "Making a Progressive Web App" section of the CRA docs. I'll work on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking something like
// If you want your app to work offline and load faster, you can change “disable” below to “enable”.
// Note this comes with some deployment pitfalls.
// Learn more about service workers: <...>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it helps, https://developers.google.com/web/fundamentals/primers/service-workers/ attempts to be a one-stop landing page for a service worker overview, and https://developers.google.com/web/progressive-web-apps/ is a similar landing page for PWAs in general.
I've updated the comments about using Service Worker in index.js. Currently the link points to the "Making a Progressive Web App" section of the CRA docs. |
Note that is in comment (If you want your app to work offline and load faster...) |
Yep, we’ll update the docs before the release. |
* WIP disable service worker by default (facebook#2554) * Updated service worker registration * Readd default export in registerServiceWorker.js * Updated comments about using Service Worker * Call it serviceWorker * Nits
+1 for this. Deployed to an HTTP site and my script is erroring because of service worker |
Any idea when this will be released? Nobody removes it where I work and then it causes all sorts of problems in staging that the senior devs have to fix. |
This will be released as part of 2.0 release. Note that this only affects the new app template, that means if an app was already bootstrapped with service worker, upgrade to 2.0 will not have any impact. If you have an existing worker that makes uses of service workers in production, you'll have to manually clean it up by unregistering. |
* WIP disable service worker by default (facebook#2554) * Updated service worker registration * Readd default export in registerServiceWorker.js * Updated comments about using Service Worker * Call it serviceWorker * Nits
Addresses disabling service worker by default from #2554.